<?php

/*
 * This will be the Create Department page. 
 * 
 * Created by: Noan Babao
 * Date created: November 28, 2011
 */

    include("siteTop.php"); 
    include('includes/siteConfig.php'); 

    // connect to database
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
?>

<!-- HEADER START -->
    <!--JAVASCRIPT SOURCES-->
    <script src="js/jquery-1.6.2.min.js"></script>
    
    <!--ACTUAL JS SCRIPTS AND JQUERIES-->
    <script type="text/javascript">
        jQuery(document).ready(function(){ 
            
         
           
           
          jQuery(".documentSetupId").blur(function(){
          
             jQuery.ajax({
                    type : 'POST',
                    url : 'ajaxRequests/admCheckDocumentSetupId.php',
                    dataType : 'json',
                    data: {
                            documentSetupId : jQuery(".documentSetupId").val()
                    },
                    success : function(data){
                        if(!data)
                        {
                            alert("Document Setup Id already exists.");
                            jQuery(".documentSetupId").val("");
                            jQuery(".documentSetupId").focus();
                        }
                        
                    },
                    error : function() {
                        alert("fail ajax checkdocumentSetup.php");
                    }
             }); 
          });
           
          
          jQuery("#createButton").click(function(){
            flag=1;
            jQuery(".required").each( function(){
                if(this.value==""){
                    flag=0;
                }
                
               
                    
                    
            });
            if(flag){
                
                
                      jQuery.ajax({
                            type : 'POST',
                            url : 'ajaxRequests/admAddDocument.php',
                            dataType : 'json',
                            data: {
                                   documentSetupId : jQuery("#documentSetupId").val() ,
                                   docType : jQuery("#docType").val() ,
                                   documentName : jQuery("#documentName").val() ,
                                   description : jQuery("#description").val() 
                                   
                            },
                            success : function(data){
                                if(!flag){
                                    alert("fail in creating accountdeptpos");
                                }else{
                                    jQuery("#formAccount").append("<input type='hidden' name='docSuccessMessage' value='Successfully created a Document with Document SetupId "+ jQuery("#documentSetupId").val()+".'> ");
                                    jQuery("#formAccount").submit();
                                }
                                    
                                

                            },
                            error : function() {
                                alert("fail ajax getdepartmentList.php");
                            }
                     });  
  
            }
            else{
                alert("Please fill up all information.");
            }
            
          });
           
           
        }); // end document ready
       
       
    </script>
<!-- HEAD END -->

<div id="content">
<h1>Create Document</h1>
    <div class="contentbox">       
        <?php
        // Check permission
        $thePermissions = array('DOCSTP');
        if ( checkPermission( $thePermissions, $userId ) == true ) {
        ?>
            <span class="screenlet" >Document Information</span>
            <form id="formAccount" name="formAccount" action="admDocumentSetup.php" method="Post" > 
                <table style="text-align:right;">
                    <tr>
                        <td > Document Setup ID : </td>
                        <td>
                            <input  style="float:left" type="text" class="required documentSetupId" value="" name="documentSetupId" id="documentSetupId" />
                        </td>
                    </tr>
                    <tr>
                        <td> Type : </td>
                        <td>
                            <Select  style="float:left" id="docType">
                                <option value="Attachment">
                                    Attachment
                                </option>
                                <option value="MainDocument">
                                    Main Document
                                </option>
                            </select>
                            
                        </td>
                    </tr>
                    <tr>
                        <td>Document Name : </td>
                        <td>
                            <input  style="float:left" type="text" class="required" value="" name="documentName" id="documentName" />
                        </td>
                    </tr>
                    <tr>
                        <td>Description : </td>
                        <td>
                            <textarea rows="4" cols="30" class="required" value="" name="description" id="description" >
                            </textarea>

                        </td>
                    </tr>
                    
                </table>
                
                
                
                <input type="button" value="Create" id="createButton" class="siteGreenButton">
                <input type="submit" value="Cancel" id="cancelButton" class="siteRedButton">
            </form>
        <?php
        }
        else {
            echo "<div class='noPermissionMsgBox noPermissionMsg' style='border: 2px solid red'><div id='noPermissionMsgs'>You have no permission to view this page.</div></div>";
        }
        // End checking permission
        ?>
    </div> <!-- contentbox end div -->
    <div style="position:relative; visibility: hidden">end</div>    <!-- extra space for contentbox -->
</div> <!-- content end div -->
<?php
    include ("siteBottom.php"); 
?>

